/**
 * 
 * doms
*/
const tabs = document.querySelector('.tabs')
const btns = tabs.getElementsByTagName('button')
const frame = document.getElementById('ssc-imageview-frame')

/****
 * iframe urls
 *  */ 
const urls = [null, null, null]

/**
 *
 * @method makeTargetUrl 外产品联查NCC单据适配
 * @returns {string} 打开ncc单据url
 */
function makeTargetUrl({
  origin,
  appcode,
  pagecode,
  appid,
  menu,
  appUrl,
}) {
  const MAIN_URL = `${origin}/nccloud/resources/workbench/public/common/main/index.html#/ifr?` //首页地址
  const PAGE_PARAM = `ifr=` +
                      encodeURIComponent(encodeURIComponent(
                        `/nccloud/resources/${appUrl}`
                      )) //页面地址参数
  const SYS_PARAM = `&c=${encodeURIComponent(encodeURIComponent(appcode))}` +      // appcode
                    `&p=${encodeURIComponent(encodeURIComponent(pagecode))}` +     // pagecode
                    `&ar=${encodeURIComponent(encodeURIComponent(appid))}` +       // 应用id
                    `&n=${encodeURIComponent(encodeURIComponent(menu))}`           // 应用名称
                    // `&b1=${encodeURIComponent(encodeURIComponent(field))}` +    // 领域 面包屑1
                    // `&b2=${encodeURIComponent(encodeURIComponent(module))}` +   // 模块 面包屑2
                    // `&b3=${encodeURIComponent(encodeURIComponent(menuclass))}`  // 节点 面包屑3
  return MAIN_URL + PAGE_PARAM + SYS_PARAM
}

/**
 * 
 * 注册页签点击事件
*/
tabs.addEventListener('click', ev => {
  ev = ev || window.event
  if (!ev.target || ev.target.className.indexOf('tab-btn') === -1) return
  let idx = -1
  while (++idx < btns.length) {
    btns[idx].className = 'tab-btn'
  }
  ev.target.className = 'tab-active tab-btn'
  const IDX = ev.target.getAttribute('btn-idx')
  frame.src = urls[IDX] || ''
})

/****
 * 
 * 初始化缓存获取 & 处理
 * */
const NCC_ImageViewFrame = sessionStorage.getItem('NCC_ImageViewFrame')
if (NCC_ImageViewFrame) {
  const {
    paraStr, factoryCode, ipAddr, origin, multiLang, pk_billid,
    data: {
      imagCount, invoiceCount, fileCount,
    },
  } = JSON.parse(NCC_ImageViewFrame)

  // 标题
  document.title = multiLang['1056-VIEWFRAME-001'] || '影像查看'
  
  // 首页签 影像查看
  frame.src = paraStr
  urls[0] = paraStr
  // btns' name
  btns[0].innerText = `${multiLang['1056-VIEWFRAME-001'] || '影像查看'} ${imagCount}`
  btns[1].innerText = `${multiLang['1056-VIEWFRAME-002'] || '电子发票'} ${invoiceCount}`
  btns[2].innerText = `${multiLang['1056-VIEWFRAME-003'] || '附件'} ${fileCount}`
  
  // 外产品联查NCC单据适配
  urls[1] = makeTargetUrl({ // invoice
    origin,
    appUrl: `sscrp/rppub/components/invoiceViewFrame/index.html#?pk_billid=${pk_billid}`,
    appcode: `10540FPCK`,
    pagecode: `10540FPCK_C`,
    appid: `1001Z010000000007YJI`,
    menu: multiLang['1056-VIEWFRAME-004'] || '电子发票查看',
  })
  urls[2] = makeTargetUrl({ // file
    origin,
    appUrl: `sscrp/rppub/components/fileViewFrame/index.html#?pk_billid=${pk_billid}`,
    appcode: `10540FJCK`,
    pagecode: `10540FJCK_C`,
    appid: `1001Z010000000007YJH`,
    menu: multiLang['1056-VIEWFRAME-005'] || '附件查看',
  })
}

